package com.atung.parking.mapstruct;

import com.atung.parking.entity.BusApprovalRecord;
import com.atung.parking.entity.BusParkingIot;
import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.export.ParkingIotExportVO;
import com.atung.parking.mapper.BusSuperUserMapper;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.ParkingIotResponse;
import com.atung.parking.response.page.SimpleParkingIotResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Administrator
 */
@Component
public class ParkingIotConverterDecorator implements ParkingIotConverter {
    @Resource
    private BusSuperUserMapper busSuperUserMapper;

    @Override
    public ParkingIotResponse toResponse(BusParkingIot busParkingIot) {
        if (busParkingIot == null) {
            return null;
        }

        ParkingIotResponse parkingIotResponse = new ParkingIotResponse();

        parkingIotResponse.setCreatorTime(timestampToString(busParkingIot.getCreatorTime()));
        parkingIotResponse.setApprovalStatus(setApprovalStatus(busParkingIot.getApprovalStatus()));
        parkingIotResponse.setId(busParkingIot.getId());
        if (busParkingIot.getSuperUserId() != null) {
            parkingIotResponse.setSuperUserId(busParkingIot.getSuperUserId());
        }
        parkingIotResponse.setContactPhone(busParkingIot.getContactPhone());
        parkingIotResponse.setName(busParkingIot.getName());
        parkingIotResponse.setTotalSpaces(busParkingIot.getTotalSpaces());
        if (busParkingIot.getRatePerHour() != null) {
            parkingIotResponse.setRatePerHour(busParkingIot.getRatePerHour().doubleValue());
        }
        parkingIotResponse.setImagePath(busParkingIot.getImagePath());
        parkingIotResponse.setAddress(busParkingIot.getAddress());
        parkingIotResponse.setDetailedAddress(busParkingIot.getDetailedAddress());
        parkingIotResponse.setHardwareVendor(busParkingIot.getHardwareVendor());
        parkingIotResponse.setCreatorId(busParkingIot.getCreatorId());
        BusSuperUser busSuperUser = busSuperUserMapper.selectById(busParkingIot.getSuperUserId());
        if (Objects.nonNull(busSuperUser)) {
            parkingIotResponse.setSuperName(busSuperUser.getName());
        }
        return parkingIotResponse;
    }

    @Override
    public SimpleParkingIotResponse toSimpleResponse(BusParkingIot busParkingIot) {
        if (busParkingIot == null) {
            return null;
        }
        SimpleParkingIotResponse simpleParkingIotResponse = new SimpleParkingIotResponse();
        simpleParkingIotResponse.setName(busParkingIot.getName());
        simpleParkingIotResponse.setId(busParkingIot.getId());

        return simpleParkingIotResponse;
    }

    @Override
    public ParkingIotExportVO toExportVo(BusParkingIot busParkingIot) {
        ParkingIotExportVO parkingIotExportVO = new ParkingIotExportVO();

        parkingIotExportVO.setId(String.valueOf(busParkingIot.getId()));
        parkingIotExportVO.setContactPhone(busParkingIot.getContactPhone());
        parkingIotExportVO.setName(busParkingIot.getName());
        parkingIotExportVO.setTotalSpaces(String.valueOf(busParkingIot.getTotalSpaces()));
        if (busParkingIot.getRatePerHour() != null) {
            parkingIotExportVO.setRatePerHour(busParkingIot.getRatePerHour().toString());
        }
        parkingIotExportVO.setAddress(busParkingIot.getAddress());
        parkingIotExportVO.setDetailedAddress(busParkingIot.getDetailedAddress());
        parkingIotExportVO.setHardwareVendor(busParkingIot.getHardwareVendor());
        if (busParkingIot.getApprovalStatus() != null) {
            parkingIotExportVO.setApprovalStatus(String.valueOf(busParkingIot.getApprovalStatus()));
        }
        parkingIotExportVO.setCreatorTime(busParkingIot.getCreatorTime());
        BusSuperUser busSuperUser = busSuperUserMapper.selectById(busParkingIot.getSuperUserId());
        if (Objects.nonNull(busSuperUser)) {
            parkingIotExportVO.setSuperName(busSuperUser.getName());
        }
        return parkingIotExportVO;
    }

    @Override
    public ApprovalRecordResponse toApprovalResponse(BusApprovalRecord busApprovalRecord) {
        if (busApprovalRecord == null) {
            return null;
        }

        ApprovalRecordResponse approvalRecordResponse = new ApprovalRecordResponse();

        approvalRecordResponse.setId(busApprovalRecord.getId());
        approvalRecordResponse.setApprovalType(busApprovalRecord.getApprovalType());
        approvalRecordResponse.setCreatorName(busApprovalRecord.getCreatorName());
        approvalRecordResponse.setCreatorTime(busApprovalRecord.getCreatorTime());

        return approvalRecordResponse;
    }
}
